/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import dao.Accounts;
import dao.ListCategory;
import dao.ListSearchVacancy;
import dao.ListWorkPlace;
import dao.loginDao;
import entities.Category;
import entities.SearchVacancy;
import entities.WorkPlace;
import entities.login;
import entities.userName;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Jung Jin Hyuk
 */
@WebServlet(name = "account", urlPatterns = {"/account"})
public class account extends HttpServlet {

    loginDao lgd = new loginDao();
    userName use = null;
    ListCategory daoCate = new ListCategory();
    ListWorkPlace daoWP = new ListWorkPlace();
    ListSearchVacancy daoVaHot = new ListSearchVacancy();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet account</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet account at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        
        List<Category> listC = daoCate.getListCategory();
        List<WorkPlace> listWP = daoWP.getListWorkPlace();
        List<SearchVacancy> lisva = daoVaHot.getlistHot();
        request.setAttribute("listC", listC);
        request.setAttribute("listWP", listWP);
        request.setAttribute("lisVa", lisva);
        request.getRequestDispatcher("user/account.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        
        Accounts ac = new Accounts();
        String acc = request.getParameter("txtAc");
        String pas = request.getParameter("txtPass");
        String full = request.getParameter("txtFullname");
        String bir = request.getParameter("txtBirthday");
        String gender = request.getParameter("txtGender");
        String adres = request.getParameter("txtAđress");
        String email = request.getParameter("txtEmail");
        String p = request.getParameter("txtPhone");
        int phone = Integer.parseInt(p);
        boolean kt = lgd.checktk(acc);
        String eror = null;
        String eror1=acc;
        String eror2=email;
        String eror3=full;
        String eror4=bir;
        String eror5=adres;
        String eror6=p;
     
        if (kt) {
            eror = "Account Exists";
            request.setAttribute("eror", eror);
            request.setAttribute("eror1", eror1);
            request.setAttribute("eror2", eror2);
            request.setAttribute("eror3", eror3);
            request.setAttribute("eror4", eror4);
            request.setAttribute("eror5", eror5);
            request.setAttribute("eror6", eror6);
            request.getRequestDispatcher("user/account.jsp").forward(request, response);
        } else {
            try {
                ac.insertAccount(acc, pas, full, bir, gender, adres, email, phone);
                request.getRequestDispatcher("user/login.jsp").forward(request, response);
            } catch (Exception e) {
                eror = "Account unsuccessful";
                request.setAttribute("eror", eror);
                request.getRequestDispatcher("user/account.jsp").forward(request, response);
            }


        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
